package com.fireline.beauty.product;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import com.zgw.fireline.base.frame.FunctionDialog;
import com.zgw.fireline.base.frame.IFunction;
import com.zgw.fireline.base.widgets.BatchItem;
import com.zgw.fireline.base.widgets.ControlUtil;

import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
import com.zgw.fireline.base.controls.DataCombo;
import com.zgw.fireline.base.dataset.Dataset;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.widgets.TableColumn;
import com.zgw.fireline.base.IDataBaseProvide;
import com.fireline.beauty.ContextHelp;
import com.fireline.beauty.bean.CpkcVo;
import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.zgw.fireline.base.controls.DataTable;
import com.zgw.fireline.base.controls.DataTableColumn;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import com.zgw.fireline.base.widgets.Batch;

/**
 * 产品入库
 * */
public class CprkEdit implements IFunction {

	private Shell shell;
	private Text textRkdh;
	private Text textSfje;
	private Text textBZ;
	private Text text_2;
	private Table table;
	/**
	 * @wbp.nonvisual location=47,602
	 */
	private final Dataset cpxxSet = new Dataset(
			"select * ,\r\n(kcsl*cbj) kczj --存库总价\r\n from cpxx \r\nwhere \r\njy<>1 and\r\n? is null  or cpbh =? or cpmc like '%'||?||'%'  or jm like '%'||?||'%'  \r\n",
			SqliteJdbcImpl.INSTANCE);
	private Tree tree;
	private Text textYfje;
	private DataCombo comboRklx;
	private static Map<String, Integer> mapRklx = new HashMap<String, Integer>();
	static {
		mapRklx.put("产品进货", 1);
		mapRklx.put("产品报溢", 2);
		mapRklx.put("产品还货", 3);
		mapRklx.put("其它入库", 4);
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	@Override
	public Control createControl(Composite parent) {
		this.shell = parent.getShell();
		cpxxSet.setKeyColumns(new String[] { "cpbh" });
		Composite content = new Composite(parent, SWT.NONE);
		content.setLayout(new GridLayout(1, false));

		Group group = new Group(content, SWT.NONE);
		group.setText("入库信息");
		GridLayout gl_group = new GridLayout(6, false);
		gl_group.verticalSpacing = 10;
		gl_group.marginHeight = 10;
		gl_group.marginLeft = 20;
		gl_group.marginBottom = 5;
		gl_group.horizontalSpacing = 0;
		group.setLayout(gl_group);
		group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));

		Label label = new Label(group, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label.setText("入库单号：");

		textRkdh = new Text(group, SWT.BORDER | SWT.READ_ONLY);
		textRkdh.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		textRkdh.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		GridData gd_textRkdh = new GridData(SWT.FILL, SWT.FILL, false, false,
				1, 1);
		gd_textRkdh.widthHint = 150;
		textRkdh.setLayoutData(gd_textRkdh);

		Label lblNewLabel_4 = new Label(group, SWT.NONE);
		GridData gd_lblNewLabel_4 = new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1);
		gd_lblNewLabel_4.horizontalIndent = 35;
		lblNewLabel_4.setLayoutData(gd_lblNewLabel_4);
		lblNewLabel_4.setText("应付金额：");

		textYfje = new Text(group, SWT.BORDER | SWT.READ_ONLY);
		textYfje.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		textYfje.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		textYfje.setText("0");
		ControlUtil.setCurrencyRule(textYfje, false, false);
		GridData gd_textYfje = new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1);
		gd_textYfje.widthHint = 100;
		textYfje.setLayoutData(gd_textYfje);
		Label lblNewLabel_1 = new Label(group, SWT.NONE);
		GridData gd_lblNewLabel_1 = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_lblNewLabel_1.horizontalIndent = 35;
		lblNewLabel_1.setLayoutData(gd_lblNewLabel_1);
		lblNewLabel_1.setText("实付金额：");

		textSfje = new Text(group, SWT.BORDER);
		textSfje.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		textSfje.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		textSfje.setText("0");
		GridData gd_txtSfje = new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1);
		gd_txtSfje.widthHint = 100;
		textSfje.setLayoutData(gd_txtSfje);
		ControlUtil.setCurrencyRule(textSfje, false, false);
		ControlUtil.setEnterToFocusMove(textSfje);

		Label lblNewLabel = new Label(group, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblNewLabel.setText("入库类型：");

		comboRklx = new DataCombo(group, SWT.READ_ONLY, (Dataset) null);
		comboRklx.setItems(new String[] { "产品进货", "产品报溢", "产品还货", "其它入库" });
		comboRklx.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		comboRklx.select(0);
		ControlUtil.setEnterToFocusMove(comboRklx);
		Label lblNewLabel_2 = new Label(group, SWT.NONE);
		lblNewLabel_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblNewLabel_2.setText("备    注：");

		textBZ = new Text(group, SWT.BORDER);
		GridData gd_text_3 = new GridData(SWT.FILL, SWT.CENTER, false, false,
				3, 1);
		gd_text_3.widthHint = 0;
		textBZ.setLayoutData(gd_text_3);
		ControlUtil.setEnterToFocusMove(textBZ);
		group.setTabList(new Control[] { textSfje, comboRklx, textBZ });

		SashForm composite_1 = new SashForm(content, SWT.NONE);
		composite_1.setSashWidth(5);
		GridData gd_composite_1 = new GridData(SWT.FILL, SWT.FILL, true, true,
				1, 1);
		gd_composite_1.verticalIndent = 5;
		composite_1.setLayoutData(gd_composite_1);
		GridLayout gl_composite_1 = new GridLayout(2, false);
		composite_1.setLayout(gl_composite_1);

		Group composite_2 = new Group(composite_1, SWT.NONE);
		composite_2.setText("产品列表");

		GridLayout gl_composite_2 = new GridLayout(2, false);
		gl_composite_2.horizontalSpacing = 0;
		composite_2.setLayout(gl_composite_2);
		GridData gd_composite_2 = new GridData(SWT.LEFT, SWT.FILL, false, true,
				1, 1);
		gd_composite_2.widthHint = 150;
		composite_2.setLayoutData(gd_composite_2);

		text_2 = new Text(composite_2, SWT.BORDER);
		text_2.setMessage("输入产品编号、名称查找");
		text_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));

		Label lblNewLabel_3 = new Label(composite_2, SWT.NONE);
		lblNewLabel_3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				doSearchCp();
			}
		});
		GridData gd_lblNewLabel_3 = new GridData(SWT.LEFT, SWT.TOP, false,
				false, 1, 1);
		gd_lblNewLabel_3.horizontalIndent = 5;
		lblNewLabel_3.setLayoutData(gd_lblNewLabel_3);
		lblNewLabel_3.setImage(SWTResourceManager.getImage(CprkEdit.class,
				"/com/fireline/beauty/image/query.png"));

		tree = new Tree(composite_2, SWT.BORDER);
		tree.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				doAdd();
			}
		});
		tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));

		Composite composite_3 = new Composite(composite_1, SWT.NONE);
		composite_3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true,
				1, 1));
		GridLayout gl_composite_3 = new GridLayout(2, false);
		gl_composite_3.marginHeight = 0;
		gl_composite_3.marginWidth = 0;
		composite_3.setLayout(gl_composite_3);

		ToolBar toolBar = new ToolBar(composite_3, SWT.FLAT | SWT.RIGHT
				| SWT.VERTICAL);

		ToolItem tltmNewItem = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doAdd();
			}
		});
		tltmNewItem.setText("  添加  ");

		ToolItem toolItem_1 = new ToolItem(toolBar, SWT.SEPARATOR);

		ToolItem tltmNewItem_1 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doEdit();
			}
		});
		tltmNewItem_1.setText("修改");

		ToolItem toolItem_2 = new ToolItem(toolBar, SWT.SEPARATOR);

		ToolItem toolItem = new ToolItem(toolBar, SWT.NONE);
		toolItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doDelete();
			}
		});
		toolItem.setText("删除");

		Group composite_4 = new Group(composite_3, SWT.NONE);
		composite_4.setText("入库清单");
		FillLayout fl_composite_4 = new FillLayout(SWT.HORIZONTAL);
		fl_composite_4.marginWidth = 5;
		fl_composite_4.marginHeight = 5;
		composite_4.setLayout(fl_composite_4);
		composite_4.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true,
				1, 1));

		table = new Table(composite_4, SWT.BORDER | SWT.FULL_SELECTION);
		table.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				doEdit();
			}
		});
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(74);
		tblclmnNewColumn.setText("产品编号");

		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(100);
		tblclmnNewColumn_1.setText("产品名称");

		TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_2.setWidth(45);
		tblclmnNewColumn_2.setText("单位");

		TableColumn tblclmnNewColumn_3 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_3.setWidth(50);
		tblclmnNewColumn_3.setText("单价");

		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(54);
		tableColumn.setText("数量");

		TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
		tableColumn_1.setWidth(54);
		tableColumn_1.setText("金额");

		TableColumn tableColumn_2 = new TableColumn(table, SWT.NONE);
		tableColumn_2.setWidth(100);
		tableColumn_2.setText("备注");
		composite_1.setWeights(new int[] { 221, 493 });

		Composite composite = new Composite(content, SWT.NONE);
		composite.setLayout(new GridLayout(2, false));
		composite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));

		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSave();
			}
		});
		GridData gd_btnNewButton = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton.widthHint = 80;
		btnNewButton.setLayoutData(gd_btnNewButton);
		btnNewButton.setText("保存");

		Button btnNewButton_1 = new Button(composite, SWT.NONE);
		GridData gd_btnNewButton_1 = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton_1.widthHint = 80;
		btnNewButton_1.setLayoutData(gd_btnNewButton_1);
		btnNewButton_1.setText("关闭");
		setParams();
		setValues();
		createGUIAfter();
		initializeData();
		return content;
	}

	protected void doDelete() {
		if (table.getSelectionCount() > 0) {
			table.getItems()[0].dispose();
			convertZje();
		}

	}

	// 入库单号
	/**
	 * 保存 入库单
	 * */
	protected void doSave() {
		Batch batch = new Batch(SqliteJdbcImpl.INSTANCE);
		BatchItem addRkd = new BatchItem(batch);
		addRkd.setName("添加入库单");
		addRkd.setCommand("INSERT INTO rkd(id, fdbh, rkrq, yfje, sfje, yhje, qkje, jbr, czy, rklx, rksm, bz)"
				+ "VALUES(?,?,?,?,?,?,?,?,?,?,?,?)");
		addRkd.setParam(0, textRkdh.getText());
		addRkd.setParam(1, "01");
		addRkd.setParam(2,
				SimpleDateFormat.getDateTimeInstance().format(new Date()));
		addRkd.setParam(3, textYfje.getText());
		addRkd.setParam(4, textSfje.getText());

		addRkd.setParam(5, "0.00");// 优惠金额
		addRkd.setParam(6, "0.00"); // 欠款金额
		addRkd.setParam(7, ""); // jbr
		addRkd.setParam(8, ContextHelp.getUserName()); //
		addRkd.setParam(9, mapRklx.get(comboRklx.getText()));
		addRkd.setParam(10, comboRklx.getText());
		addRkd.setParam(11, textBZ.getText());

		for (TableItem item : table.getItems()) {
			BatchItem addRkcp = new BatchItem(batch);
			CpkcVo kcvo = (CpkcVo) item.getData();
			addRkcp.setName("添加入库单产品明细");
			addRkcp.setCommand("INSERT INTO rksp ( rkid, cpid, dj, sl, zje, bz)"
					+ "VALUES(?,?,?,?,?,?)");
			addRkcp.setParam(0, textRkdh.getText());
			addRkcp.setParam(1, kcvo.cpbh);
			addRkcp.setParam(2, kcvo.dj);
			addRkcp.setParam(3, kcvo.sl);
			addRkcp.setParam(4, kcvo.getZje());
			addRkcp.setParam(5, kcvo.bz);
			// 修改产品 信息
			BatchItem editCp = new BatchItem(batch);
			editCp.setName("修改产品明细");
			editCp.setCommand("update cpxx set cbj=round((cbj*kcsl+?*?)/(kcsl+?),2),zjjj=?, kcsl=kcsl+? where cpbh=?");
			editCp.setParam(0, kcvo.dj);
			editCp.setParam(1, kcvo.sl);
			editCp.setParam(2, kcvo.sl);
			editCp.setParam(3, kcvo.dj);
			editCp.setParam(4, kcvo.sl);
			editCp.setParam(5, kcvo.cpbh);
		}
		batch.execute();

		MessageDialog.openInformation(shell, "提示", "保存成功");
		initializeData();
	}

	protected void doEdit() {
		if (table.getSelectionCount() > 0) {
			TableItem item = table.getSelection()[0];
			CpkcVo kc = (CpkcVo) item.getData();
			cpxxSet.setSelected(kc.cpbh);
			CpkcDialog dlg = new CpkcDialog(shell, SWT.NONE, cpxxSet, 1);
			dlg.kcvo = kc;
			if (dlg.open() == SWT.OK) {
				kc = dlg.kcvo;
				item.setText(0, toString(kc.cpbh));
				item.setText(1, toString(cpxxSet.getString("cpmc")));
				item.setText(2, toString(cpxxSet.getString("dw")));
				item.setText(3, toString(kc.dj));
				item.setText(4, String.valueOf(kc.sl));
				item.setText(5, kc.getZje());
				item.setText(6, toString(kc.bz));
				item.setData(kc);
			}
		}
		convertZje();
	}

	protected void doAdd() {
		String cpbh = null;
		if (tree.getSelectionCount() > 0) {
			cpbh = (String) tree.getSelection()[0].getData();
			if (cpbh == null) {
				return;
			}
		}
		cpxxSet.setSelected(cpbh);
		CpkcDialog dlg = new CpkcDialog(shell, SWT.NONE, cpxxSet, 1);
		if (dlg.open() == SWT.OK) {
			TableItem item = new TableItem(table, SWT.NONE);
			CpkcVo kc = dlg.kcvo;
			item.setText(0, toString(kc.cpbh));
			item.setText(1, toString(cpxxSet.getString("cpmc")));
			item.setText(2, toString(cpxxSet.getString("dw")));
			item.setText(3, toString(kc.dj));
			item.setText(4, String.valueOf(kc.sl));
			item.setText(5, kc.getZje());
			item.setText(6, toString(kc.bz));
			item.setData(kc);
		}
		convertZje();
	}

	// 计算应付总金额
	private void convertZje() {
		BigDecimal dec = new BigDecimal(0);
		for (TableItem item : table.getItems()) {
			CpkcVo kc = (CpkcVo) item.getData();
			dec = dec
					.add(new BigDecimal(kc.dj).multiply(new BigDecimal(kc.sl)));
		}
		textYfje.setText(dec.toString());
		textSfje.setText(dec.toString());
	}

	private String toString(Object obj) {
		if (obj instanceof String) {
			return (String) obj;
		}
		return "";
	}

	protected void doSearchCp() {
		setParams();
		cpxxSet.update();
		builCpxxLb();
	}

	private void initializeData() {
		// 生成单据号
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmSSss");
		textRkdh.setText("RK1" + format.format(new Date()));
		textBZ.setText("");
		textSfje.setText("0");
		textYfje.setText("0");
		table.removeAll();
		builCpxxLb();
	}

	// 构建产品信息列表
	private void builCpxxLb() {
		tree.removeAll();
		// 构建产品列表
		TreeItem parent, item;
		String lb;
		Map<String, TreeItem> map = new HashMap<String, TreeItem>();
		for (int i = 0; i < cpxxSet.getCount(); i++) {
			cpxxSet.setSelected(i);
			lb = cpxxSet.getString("lb");
			lb = lb == null || lb.trim().equals("") ? "其它" : lb;
			if (!map.containsKey(lb)) {
				parent = new TreeItem(tree, SWT.NONE);
				parent.setText(lb);
				map.put(lb, parent);
			}
			item = new TreeItem(map.get(lb), SWT.NONE);
			item.setText(cpxxSet.getStringForExpr("${cpmc}(${cpbh})"));
			item.setData(cpxxSet.getString("cpbh"));
			map.get(lb).setExpanded(true);
		}
	}

	protected void setParams() {
		cpxxSet.setParam(0, text_2.getText());
		cpxxSet.setParam(1, text_2.getText());
		cpxxSet.setParam(2, text_2.getText());
		cpxxSet.setParam(3, text_2.getText());
	}

	protected void setValues() {
	}

	protected void createGUIAfter() {
		cpxxSet.update();
	}

	public static void main(String[] args) {
		IFunction cprk = new CprkEdit();
		FunctionDialog dlg = new FunctionDialog(new Shell(), cprk);
		dlg.open();
	}
}
